// Created from GroupGenerationTemplate
// For Decisions HL7 Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using DecisionsFramework.Design.Properties;
using Decisions.HL7.DataTypes;
using ST = System.String;
using SI = System.String;
using IS = System.String;
using ID = System.String;
using SNM = System.String;

namespace Decisions.HL7.Message
{
    [DataContract]
	public class OBSERVATION_REQUEST_OPL_O37 : AbstractGroup
    {
        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("31 ORC",31)]
  	    [HL7Field(HL7Version.v26, 31, HL7Repeat.False, HL7Required.True,"ORC")]
	    public ORC ORC {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("32 OBR",32)]
  	    [HL7Field(HL7Version.v26, 32, HL7Repeat.False, HL7Required.True,"OBR")]
	    public OBR OBR {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("33 ROL",33)]
  	    [HL7Field(HL7Version.v26, 33, HL7Repeat.True, HL7Required.False,"ROL")]
	    public ROL[] ROL {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("34 TIMING",34)]
  	    [HL7Field(HL7Version.v26, 34, HL7Repeat.True, HL7Required.False,"TIMING_OPL_O37")]
	    public TIMING_OPL_O37[] TIMING {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("38 TCD",38)]
  	    [HL7Field(HL7Version.v26, 38, HL7Repeat.False, HL7Required.False,"TCD")]
	    public TCD TCD {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("39 DG1",39)]
  	    [HL7Field(HL7Version.v26, 39, HL7Repeat.True, HL7Required.False,"DG1")]
	    public DG1[] DG1 {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("40 OBX",40)]
  	    [HL7Field(HL7Version.v26, 40, HL7Repeat.True, HL7Required.False,"OBX")]
	    public OBX[] OBX {get;set;}



		#region Parser Helpers and Conrete Impls

        internal override void ConsumeTextSegmentsForGroup(Stack<string> availableData, HL7ParserOptions options) {

			ORC = PopSegmentParseAndCheckRequired<ORC>(availableData, options, "ORC");
			OBR = PopSegmentParseAndCheckRequired<OBR>(availableData, options, "OBR");
			ROL = PopSegmentParseAndCheckRequiredArray<ROL>(availableData, options, "ROL");
			TIMING = ProcessGroupArray<TIMING_OPL_O37>(availableData, options, "TIMING");
			TCD = PopSegmentParseAndCheckRequired<TCD>(availableData, options, "TCD");
			DG1 = PopSegmentParseAndCheckRequiredArray<DG1>(availableData, options, "DG1");
			OBX = PopSegmentParseAndCheckRequiredArray<OBX>(availableData, options, "OBX");
        }

		internal override bool CanConsume(string availableData, HL7ParserOptions options)
        {
			bool isORC = availableData.StartsWith("ORC", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("ORC", options.Version) || isORC) {
				return isORC;
			}
			bool isOBR = availableData.StartsWith("OBR", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("OBR", options.Version) || isOBR) {
				return isOBR;
			}
			bool isROL = availableData.StartsWith("ROL", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("ROL", options.Version) || isROL) {
				return isROL;
			}
			TIMING_OPL_O37 testTIMING_OPL_O37 = new TIMING_OPL_O37();
			bool grpCCTIMING_OPL_O37 = (testTIMING_OPL_O37.CanConsume(availableData, options));
			if (IsRequired("TIMING", options.Version) || grpCCTIMING_OPL_O37) {
				return grpCCTIMING_OPL_O37;
			}
			bool isTCD = availableData.StartsWith("TCD", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("TCD", options.Version) || isTCD) {
				return isTCD;
			}
			bool isDG1 = availableData.StartsWith("DG1", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("DG1", options.Version) || isDG1) {
				return isDG1;
			}
			bool isOBX = availableData.StartsWith("OBX", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("OBX", options.Version) || isOBX) {
				return isOBX;
			}
			return false;
        }

        private static Dictionary<string, bool> SEGMENT_VERSION_REQUIRED_MAP = new Dictionary<string, bool>();

		protected override Dictionary<string, bool> MAP_OF_SEGMENT_TO_REQUIRED
        {
            get
			{
				return SEGMENT_VERSION_REQUIRED_MAP;
			}
        }

		public override string HL7ENTITY_TYPE_NAME
        {
            get {
				return "OBSERVATION_REQUEST";
			}
        }

        static OBSERVATION_REQUEST_OPL_O37() {

             SetupStaticPropertyMap(SEGMENT_VERSION_REQUIRED_MAP, typeof(OBSERVATION_REQUEST_OPL_O37));

        }

		#endregion

		public override string GetER7(HL7ParserOptions options)
		{
			List<string> segmentData = new List<string>();
		 if (ORC != null) {
			string segmentValue = ORC.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (OBR != null) {
			string segmentValue = OBR.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (ROL != null && ROL.Length > 0) {
			for (int i = 0; i < ROL.Length; i++) {
				string segmentValue = ROL[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (TIMING != null && TIMING.Length > 0) {
			for (int i = 0; i < TIMING.Length; i++) {
				string segmentValue = TIMING[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (TCD != null) {
			string segmentValue = TCD.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (DG1 != null && DG1.Length > 0) {
			for (int i = 0; i < DG1.Length; i++) {
				string segmentValue = DG1[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (OBX != null && OBX.Length > 0) {
			for (int i = 0; i < OBX.Length; i++) {
				string segmentValue = OBX[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
           return string.Join(HL7ParserConstants.SEGMENT_SEPARATOR, segmentData.ToArray());
        
        }
    }
}

